//
//  TSYNetworkManager.swift
//  TSYWeiBoDemo
//
//  Created by apple on 2016/11/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import Foundation
import AFNetworking

enum TSYHTTPMethod{
    case GET
    case POST
    
}


class TSYNetworkManager: AFHTTPSessionManager {
    
    static let shared = TSYNetworkManager()
    
    
    
    var acessToken:String? = "22"
    
    func tokenRequest(method:TSYHTTPMethod = .GET,URLString:String,parameters:[String:AnyObject]?,completion:(_ json:AnyObject?,_ isSuccess:Bool)->()) {
        
        guard let token = acessToken else {
            print("没有")
            
            completion(nil, false)
            
            return
        }
        
        var parameters = parameters
         
        if  parameters == nil {
            parameters = [String:AnyObject]()
        }
        
        parameters!["access_token"] = token as AnyObject?
        
        //发送网络请求
    }
    
    // 将成功和失败的回调写在一个逃逸闭包中
    func request(requestType : TSYHTTPMethod, url : String, parameters : [String : Any], resultBlock : @escaping([String : Any]?, Error?) -> ()) {
        
        // 成功闭包
        let successBlock = { (task: URLSessionDataTask, responseObj: Any?) in
            resultBlock(responseObj as? [String : Any], nil)
        }
        
        // 失败的闭包
        let failureBlock = { (task: URLSessionDataTask?, error: Error) in
            resultBlock(nil, error)
        }
        
        // Get 请求
        if requestType == .GET {
            get(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
        // Post 请求
        if requestType == .POST {
            post(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
    }
    
    // 将成功和失败的回调分别写在两个逃逸闭包中
    func request(requestType : TSYHTTPMethod, url : String, parameters : [String : Any], succeed : @escaping([String : Any]?) -> (), failure : @escaping(Error?) -> ()) {
        
        // 成功闭包
        let successBlock = { (task: URLSessionDataTask, responseObj: Any?) in
            succeed(responseObj as? [String : Any])
        }
        
        // 失败的闭包
        let failureBlock = { (task: URLSessionDataTask?, error: Error) in
            failure(error)
        }
        
        // Get 请求
        if requestType == .GET {
            get(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
        // Post 请求
        if requestType == .POST {
            post(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
    }

    
    ///上传文件（图片 ）可能还要分装 token
    func upload(url:String,parameters:[String:AnyObject]?,name:String,data:Data,completions:@escaping (_ json:Any?,_ isSuccess:Bool)->()){
        
        post(url, parameters: parameters, constructingBodyWith: {(formData) in
                formData.appendPart(withFileData: data, name: name, fileName: "image", mimeType: "application/octet-stream")
            
        }, progress:{ (Progress) in
                
        }, success: {(URLSessionDataTask, success) in
            
                completions(success, true)
            
        }, failure: {(URLSessionDataTask, Error) in
            
                completions(nil, false)
            
        })
        
        
    }
    
}
